package crm.view.desktop.iFrame.views;

import java.util.ArrayList;

import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import crm.model.teacherPC.CRMListener;
import crm.model.teacherPC.Manager;
import crm.model.teacherPC.net.PcWindow;
import crm.model.teacherPC.net.StudentPC;



public class WindowList implements CRMListener, ListModel {
	StudentPC student;
	ArrayList <PcWindow>windowsList;
	ArrayList <ListDataListener> listener;
	
	public WindowList(StudentPC student, Manager manager) {

		this.student=student;
		windowsList = student.getWindows();
		listener = new ArrayList<ListDataListener>();
		manager.addListener(this);
//		for(int i=0; i<student.getWindows().size();i++){
//			addElement(student.getWindows().get(i));
//		}
	}

	public void actualisingComplete(StudentPC student) {
//		System.out.println("akt");
//		if (this.student==student){
//			System.out.println("lisator");
//			fireListener();
//		}
		if (student == this.student){
//			removeAllElements();
//			System.out.println("number of windows: " + student.getWindows().size());
//			for(int i=0; i<student.getWindows().size();i++){
//				addElement(student.getWindows().get(i));
//				System.out.println("add window: " + student.getWindows().get(i));
//			}
			fireListener();
		}
		//fireContentsChanged(this, 0, student.getWindows().size());
	}
	public void fireListener (){
		for(ListDataListener l: listener){
			l.contentsChanged(new ListDataEvent(this,ListDataEvent.CONTENTS_CHANGED,  0, student.getWindows().size()));
		}
	}
	public void connectionStarted(){

	}

	public void connectionClosed(){

	}

	public void crmExit(){

	}
	@Override
	public void exitCRM() {
		// TODO Auto-generated method stub

	}
	@Override
	public void studentAdded(StudentPC student) {
		// TODO Auto-generated method stub

	}
	@Override
	public void studentRemoved(StudentPC student) {
		// TODO Auto-generated method stub

	}

	@Override
	public int getSize() {
		return student.getWindows().size();
	}

	@Override
	public Object getElementAt(int index) {
		return student.getWindows().get(index);
	}

	@Override
	public void addListDataListener(ListDataListener l) {
		listener.add(l);
	}

	@Override
	public void removeListDataListener(ListDataListener l) {
		listener.remove(l);
	}

}
